<template>
    <van-contact-card type="add" @click="onAdd" add-text="点击新增用户" />
    <van-search v-model="searchText" @search="onSearch" placeholder="请输入搜索的用户信息" />
      <van-card
          style = "white-space: pre-wrap"
          v-for="user in listPageList" :key="user.id"
          :title="user.userAccount"
          :desc="formatDesc(user.id)"
          thumb="src/img/yln.jpg"
      >
        <template #desc>
          <div>
            id: {{ user.id }}             <br>
            性别:{{user.sex}}              <br>
            邮箱:{{user.email}}            <br>
            电话:{{user.phone}}             <br>
            用户简介:{{user.userProfile}}    <br>
          </div>
        </template>

        <template #footer>
          <van-button size="normal" @click="clickUserInfo(user.id)">用户具体信息</van-button>
          <van-button size="normal" @click="clickSearchSign(user.id)">签到信息</van-button>
        </template>
      </van-card>
  <van-pagination @change="handlePageChange()" v-model="currentPage" :total-items="listSize" :items-per-page="items_per_page" />
</template>

<script setup>
import {useRouter} from "vue-router";
import TeamCardList from "../components/TeamCardList.vue";
import {onMounted, ref} from "vue";
import myAxios from "../../plugins/myAxios";
import {showFailToast, showToast} from "vant";
import {getUserRoleIsAdmin} from "../services/userRole.ts";
import {getCurrentUser} from "../services/user.ts";
const searchText = ref('');
const router = useRouter();

const listPageList = ref('');
const listPageListTrue = ref('');
const name = ref('张三');
const items_per_page = ref(3);
const currentPage = ref(1);
const listSize = ref(10);
onMounted(async () =>{
    listPageList.value = await listPage();
})
const listPage = async () => {
    const res = await getCurrentUser();
    if (res.data.userRole === 'admin'){
        const res = await myAxios.post("/user/list/search/page",{
            current : currentPage.value,
            pageSize : items_per_page.value,
            content : searchText.value,
            userRole : 'admin'
        });
        listPageList.value = res.data.data.records;
        listSize.value = res.data.data.total;
        return res.data.data.records;
    }else if (res.data.userRole === 'superAdmin'){
        const res = await myAxios.post("/user/list/search/page",{
            current : currentPage.value,
            pageSize : items_per_page.value,
            content : searchText.value,
            userRole : 'superAdmin'
        });
        listPageList.value = res.data.data.records;
        listSize.value = res.data.data.total;
        return res.data.data.records;
    }
}
const onSearch = async () => {
    await listPage();
};
const onAdd = () => {
  router.push({
    path: "/admin/user/add"
  })
};
const formatDesc = (id) =>{
  return `用户id ${id} 测试`;
}
const handlePageChange = async ()=>{
  await listPage();
}
const clickUserInfo = (id) =>{
    router.push({
         path: '/admin/userInfo',
         query:{
             userId: id,
         }
    });
}
const clickSearchSign = (id) =>{
    router.push({
        path: '/admin/searchSign',
        query:{
            userId: id,
        }
    });
}

</script>

<style scoped>

</style>